package controller;

import entity.BaseEntity;
import entity.Product;
import service.ProductService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.Console;
import java.util.List;

/**
 * (Product)表控制层
 *
 * @author makejava
 * @since 2023-12-24 22:17:26
 */
@RestController
@RequestMapping(value = "/product", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public class ProductController {
    /**
     * 服务对象
     */
    @Autowired
    private ProductService productService;

    @GetMapping("redisProduct")
    public ResponseEntity<List<Object>> getRedisProduct() {
        List<Object> productList = this.productService.getRedisProduct();
        System.out.println("%%%%%%%" + productList);
        return ResponseEntity.ok(productList);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @PostMapping("queryByPage")
    public ResponseEntity<Page<Product>> queryByPage(@RequestBody @Validated Product product) {
        return ResponseEntity.ok(this.productService.queryByPage(product,PageRequest.of(product.getPage(),product.getSize())));
    }

    @PostMapping("queryNewProducts")
    public ResponseEntity<Page<Product>> queryNewProducts(@RequestBody @Validated Product product) {
        return ResponseEntity.ok(this.productService.queryNewProducts(product,PageRequest.of(product.getPage(),product.getSize())));
    }
    @PostMapping("searchByName")
    public ResponseEntity<Page<Product>> searchByName(@RequestBody @Validated String searchName) {
        System.out.println("++++++++++++++++"+searchName);
        return ResponseEntity.ok(this.productService.searchByName(searchName));
    }

    @PostMapping("queryProductTypeName")
    public String queryProductTypeName(@RequestBody @Validated int productTypeId) {
        return this.productService.queryProductTypeName(productTypeId);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("getById/{id}")
    public ResponseEntity<Product> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.productService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param product 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Product> add(Product product) {
        return ResponseEntity.ok(this.productService.insert(product));
    }

    /**
     * 编辑数据
     *
     * @param product 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Product> edit(Product product) {
        return ResponseEntity.ok(this.productService.update(product));
    }


    /**
     * 根据商品id将商品的状态改为删除状态
     *
     * @param proId 前台传入的id
     * @return 返回影响的行数
     */
    @GetMapping("delProductById/{proId}")
    public ResponseEntity<Integer> deleteById( @PathVariable String proId) {
        System.out.println("传入的proId"+proId);
        return ResponseEntity.ok(this.productService.deleteById(Integer.valueOf(proId)));
    }

}